﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Types;

namespace Client
{
    public partial class MainForm : Form
    {
        private LoginForm loginForm;
        private string username;
        private List<CourseType> courseList = new List<CourseType>();
        private List<QuestionType> questionList = new List<QuestionType>();

        public delegate void ShowMainFormDelegate(UserType user);
        public delegate void CloseClientDelegate();
        public delegate void UpdateCourseListDelegate(List<CourseType> courseList);
        public delegate void UpdateQuestionListDelegate(List<QuestionType> questionList);
        public delegate void AddQuestionDelegate(string summary);
        public delegate void AddAnswerDelegate(string summary);
        public delegate void DeleteQuestionDelegate(string summary);
        public delegate void ErrorMessageDelegate(string message);

        public MainForm()
        {
            InitializeComponent();
            ClientManager.Instance.showMainForm += new ClientManager.ShowMainForm(this.ShowMainFrom);
            ClientManager.Instance.closeClient += new ClientManager.CloseClient(this.CloseClient);
            ClientManager.Instance.updateCourseList += new ClientManager.UpdateCourseList(this.UpdateCourseList);
            ClientManager.Instance.updateQuestionList += new ClientManager.UpdateQuestionList(this.UpdateQuestionList);
            ClientManager.Instance.addQuestion += new ClientManager.AddQuestionDelegate(this.AddQuestion);
            ClientManager.Instance.addAnswer += new ClientManager.AddAnswerDelegate(this.AddAnswer);
            ClientManager.Instance.deleteQuestion += new ClientManager.DeleteQuestionDelegate(this.DeleteQuestion);
            ClientManager.Instance.showErrorMessage += new ClientManager.ShowErrorMessage(this.showMessageError);
        }

        private void ShowMainFrom(UserType user)
        {
            if (InvokeRequired)
            {
                Invoke(new ShowMainFormDelegate(this.ShowMainFrom), user);
            }
            else
            {
                username = user.username;
                loginForm.Visible = false;
                loginForm.Hide();
                this.Visible = true;
                currentLoggedUserLabel.Text = username;
            }
        }

        private void CloseClient()
        {
            if (InvokeRequired)
            {
                Invoke(new CloseClientDelegate(this.CloseClient));
            }
            else
            {
                MessageBox.Show("Server is down, client will exit", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                System.Environment.Exit(System.Environment.ExitCode);
            }
        }

        private void UpdateCourseList(List<CourseType> courseList)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateCourseListDelegate(this.UpdateCourseList), courseList);
            }
            else
            {
                this.courseList = courseList;
            }
        }

        private void UpdateQuestionList(List<QuestionType> questionList)
        {
            if (InvokeRequired)
            {
                Invoke(new UpdateQuestionListDelegate(this.UpdateQuestionList), questionList);
            }
            else
            {
                questionsListBox.Items.Clear();
                foreach (var question in questionList)
                {
                    questionsListBox.Items.Add(question.summary);
                }
                this.questionList = questionList;
            }
        }

        private void AddQuestion(string summary)
        {
            if (InvokeRequired)
            {
                Invoke(new AddQuestionDelegate(this.AddQuestion), summary);
            }
            else
            {
                MessageBox.Show("Question " + summary + " added successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void AddAnswer(string summary)
        {
            if (InvokeRequired)
            {
                Invoke(new AddAnswerDelegate(this.AddAnswer), summary);
            }
            else
            {
                MessageBox.Show("Answer " + summary + " added successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void DeleteQuestion(string summary)
        {
            if (InvokeRequired)
            {
                Invoke(new DeleteQuestionDelegate(this.DeleteQuestion), summary);
            }
            else
            {
                MessageBox.Show("Question " + summary + " deleted successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void showMessageError(string message)
        {
            if (InvokeRequired)
            {
                Invoke(new ErrorMessageDelegate(this.showMessageError), message);
            }
            else
            {
                MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            loginForm = new LoginForm(this);
            loginForm.Show();
            this.Visible = false;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            ClientManager.Instance.LogoutUser(username);
        }

        private void addQuestionButton_Click(object sender, EventArgs e)
        {
            AddQuestionCourseListForm addQuestionCourseListForm = new AddQuestionCourseListForm(courseList, username);
            addQuestionCourseListForm.ShowDialog();
            if (myQuestionsRadioButton.Checked)
            {
                ClientManager.Instance.GetMyQuestionList(username);
            }
            else if (allQuestionsRadioButton.Checked)
            {
                ClientManager.Instance.GetQuestionList(username);
            }
        }

        private void addAnswerButton_Click(object sender, EventArgs e)
        {
            if (questionsListBox.SelectedIndex == -1)
            {
                MessageBox.Show("Please select question to add answer to", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                QuestionType question = questionList[questionsListBox.SelectedIndex];
                AddAnswerForm addAnswerForm = new AddAnswerForm(question, username);
                ClientManager.Instance.GetAnswerList(question);
                addAnswerForm.ShowDialog();
            }
        }

        private void coursesListBox_MouseMove(object sender, MouseEventArgs e)
        {
            ShowToolTip(sender, e);
        }

        private void questionsListBox_MouseMove(object sender, MouseEventArgs e)
        {
            ShowToolTip(sender, e);
        }

        public void ShowToolTip(object sender, MouseEventArgs e)
        {
            ListBox objectListBox = (ListBox)sender;
            string toolTipString = "";
            // Get the item
            int itemIndex = objectListBox.IndexFromPoint(e.Location);
            if ((itemIndex >= 0) && (itemIndex < objectListBox.Items.Count))
            {
                toolTipString = objectListBox.Items[itemIndex].ToString();
            }
            listToolTip.SetToolTip(objectListBox, toolTipString);
        }

        private void myQuestionsRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (myQuestionsRadioButton.Checked)
            {
                ClientManager.Instance.GetMyQuestionList(username);
            }
        }

        private void allQuestionsRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            if (allQuestionsRadioButton.Checked)
            {
                ClientManager.Instance.GetQuestionList(username);
            }
        }

        private void deleteQuestionButton_Click(object sender, EventArgs e)
        {
            if (questionsListBox.SelectedIndex == -1)
            {
                MessageBox.Show("Please select question to delete", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                ClientManager.Instance.DeleteQuestion(questionList[questionsListBox.SelectedIndex]);
                if (myQuestionsRadioButton.Checked)
                {
                    ClientManager.Instance.GetMyQuestionList(username);
                }
                else if (allQuestionsRadioButton.Checked)
                {
                    ClientManager.Instance.GetQuestionList(username);
                }
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            SearchForm searchForm = new SearchForm(username);
            searchForm.ShowDialog();
        }
    }
}
